Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:14:25+00:00 2026-06-05T00:14:25+00:00

This is my JSON file shipping:{ countries:{ 150:{ id:150, code:nl, title:Nederland } }, country:150,

  • 0

This is my JSON file

"shipping":{
    "countries":{
        "150":{
            "id":150,
            "code":"nl",
            "title":"Nederland"
        }
    },
    "country":150,
    "zipcode":null,
    "methods":{
        "core|13490|40699":{
            "id":"core|13490|40699",
            "title":"ophalen",
            "description":"ophalen",
            "content":false,
            "pickup":true,
            "cod":false,
            "price":{
                "price_excl":"0.0000",
                "price_incl":"0.0000"
            }
    },
    "core|10292|40718":{
        "id":"core|10292|40718",
        "title":"Pakketdienst",
        "description":"Pakketdienst",
        "content":false,
        "pickup":false,
        "cod":false,
        "price":{
        "price_excl":"33.5714",
        "price_incl":"39.9500"}
        }
    }
}

My script looks like this:

function bakVormShipping(targetClass){
    $.getJSON('http://shop.com/cart/?format=json', function(data){
        var methods = ''
        $.each(data.cart.shipping.methods, function(index, methods){
            if (index == "core|10292|40696") {
                $('<span></span>').html('<strong>' + methods.price.price_incl + '</strong>').appendTo(targetClass);
            }
            else if (index == "core|10292|40693") {
                $('<span></span>').html('<strong>' + methods.price.price_incl + '</strong>').appendTo(targetClass);
            }
            else if (index == "core|10292|40718") {
                $('<span></span>').html('<strong>' + methods.price.price_incl + '</strong>').appendTo(targetClass);
            }
        });
    });
}

First some little explanation. The json you see is called from a “cart” page. The first method you see is shipping method: pick up at store. The second for actual shipping. I would like to load the the second one (actual shipping) on different pages in my shop. So people can see what the shipping costs are. All products are based on weight so when a products weighs more, then the shipping costs change to a method with id “core|10292|40693” and “core|10292|40718” (see the code) and have all different prices off course. All dynamically so the json will always have the pick up method and an actual shipping method.

What I try to achieve is
a) shorten the code. For example if (index == “core|10292|40696” || “core|10292|40693” || “core|10292|40718”) doesn’t work and prints out both the pick up method and the actual shipping method.

and b) Convert the output to currency with two decimals. I searched this forum but I can’t get that to work on this code. The code now prints 39.9500 instead of 39.95

c) I want to get rid of $('<span></span>') because the code now prints everything in a span. I tried using $(methods) but that gives an “undefined” error. Obviously because var methods =is “empty” and does nothing.

Does anyone have some directions or is willing to help? I’m pretty new to JSON and jquery so bear with me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:14:27+00:00Added an answer on June 5, 2026 at 12:14 am

    a) if (index == "core|10292|40696" || "core|10292|40693" || "core|10292|40718") doesn’t work

    To do something if index equals any of those values you need to do this:

    if (index == "core|10292|40696" || index == "core|10292|40693"
        || index == "core|10292|40718")
    

    b) Convert the output to currency with two decimals.

    Currently your currency amounts are strings. The following uses the unary plus operator to change price_incl from a string to a number, then uses the .toFixed() method to round them off to two decimal places:

    (+methods.price.price_incl).toFixed(2)
    

    The parentheses around +methods.price.price_incl are there because without them the method call to .toFixed(2) would have higher operator precendence than the unary plus.

    c) I want to get rid of $('<span></span>') because the code now prints everything in a span.

    You can create your strong elements the same way as you were creating the spans, i.e., by passing a string of html to the $() function (and then appending the result to targetClass like you already do):

    $('<strong>' + methods.price.price_incl + '</strong>').appendTo(targetClass);
    
    // OR, possibly easier to read but definitely more compact:
    $('<strong/>').html(methods.price.price_incl).appendTo(targetClass);
    
    // OR with the rounding included:
    $('<strong/>').html((+methods.price.price_incl).toFixed(2)).appendTo(targetClass);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this .json file: [ { id: 1, title: Ben\\'s First Blog Post,
My code like this: std::istringstream file(res/date.json); std::ostringstream tmp; tmp<<file.rdbuf(); std::string s = tmp.str(); std::cout<<s<<std::endl;
I am parsing this JSON file (correctly, it works with the UITextFields): {longitude:[37.786793,39.388528],latitude:[-122.395416,-78.887734]} ind
I have a JSON file which contains a pound symbol in it. This json
I am trying to loop through this external JSON file(locally stored), but I cannot
This the model that I want to create using json file Ext.define('Users', { extend:
I have a json file exported from phpmyadmin, it looks like this(utf-8 file): [{user_email:
I'm trying to load a .json file with this line: $.getJSON('engines.json',{},function(data){ alert(data); }); If
I have some data and need to create a json file with this structure
This works fine: var vm = {}; $.getJSON('file.json', function (data) { vm.objects = data;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.